/**
 * 
 */
package net.phoenix.repository.observation;

import java.util.List;
import java.util.Vector;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.observation.EventJournal;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.EventListenerIterator;
import javax.jcr.observation.ObservationManager;

/**
 * @author lixf
 *
 */
public class BasicObservationManager implements ObservationManager {
	
	private List<EventListenerWrapper> listeners;
	
	public BasicObservationManager(){
		this.listeners = new Vector<EventListenerWrapper>();
	}

	/* (non-Javadoc)
	 * @see javax.jcr.observation.ObservationManager#addEventListener(javax.jcr.observation.EventListener, int, java.lang.String, boolean, java.lang.String[], java.lang.String[], boolean)
	 */
	@Override
	public void addEventListener(EventListener listener, int eventTypes, String absPath, boolean isDeep, String[] uuid, String[] nodeTypeName, boolean noLocal) throws RepositoryException {
		this.listeners.add(new EventListenerWrapper(listener, eventTypes, absPath ,isDeep, uuid, nodeTypeName, noLocal));

	}
	
	
	/**
	 * @param node
	 * @param type
	 * @throws RepositoryException
	 */
	public void fire(Node node, int type) throws RepositoryException{
		EventIteratorWrapper events = new EventIteratorWrapper(new NodeEvent(node, type));
		for(EventListenerWrapper wrapper: this.listeners){
			if(wrapper.applyTo(node, type))
				wrapper.getListener().onEvent(events);
		}
	}

	public void fire(Property property, int type) throws RepositoryException{
		EventIteratorWrapper events = new EventIteratorWrapper(new PropertyEvent(property, type));
		for(EventListenerWrapper wrapper: this.listeners){
			if(wrapper.applyTo(property, type))
				wrapper.getListener().onEvent(events);
		}
	}

	/* (non-Javadoc)
	 * @see javax.jcr.observation.ObservationManager#removeEventListener(javax.jcr.observation.EventListener)
	 */
	@Override
	public void removeEventListener(EventListener listener) throws RepositoryException {
		for(EventListenerWrapper source: listeners){
			if(source.getListener().equals(listener))
				listeners.remove(source);
		}

	}

	/* (non-Javadoc)
	 * @see javax.jcr.observation.ObservationManager#getRegisteredEventListeners()
	 */
	@Override
	public EventListenerIterator getRegisteredEventListeners() throws RepositoryException {		
		return new EventListenerIteratorWrapper(this.listeners);
	}

	/* (non-Javadoc)
	 * @see javax.jcr.observation.ObservationManager#setUserData(java.lang.String)
	 */
	@Override
	public void setUserData(String userData) throws RepositoryException {
	}

	/* (non-Javadoc)
	 * @see javax.jcr.observation.ObservationManager#getEventJournal()
	 */
	@Override
	public EventJournal getEventJournal() throws RepositoryException {
		return null;
	}

	/* (non-Javadoc)
	 * @see javax.jcr.observation.ObservationManager#getEventJournal(int, java.lang.String, boolean, java.lang.String[], java.lang.String[])
	 */
	@Override
	public EventJournal getEventJournal(int eventTypes, String absPath, boolean isDeep, String[] uuid, String[] nodeTypeName) throws RepositoryException {
		return null;
	}

}
